package com.zw.yzk.kabu.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.AlbumAdapter;
import com.zw.yzk.kabu.adapter.HorizontalDividerItemDecoration;
import com.zw.yzk.kabu.adapter.VerticalDividerItemDecoration;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerAlbumComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.GetAlbumModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.AlbumModel;
import com.zw.yzk.kabu.presenter.AlbumPresenter;
import com.zw.yzk.kabu.view.view.AlbumView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/7/5.
 */

public class AlbumActivity extends BaseActivity implements AlbumView {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.list)
    RecyclerView list;

    @Inject
    AlbumPresenter presenter;
    private AlbumAdapter albumAdapter;

    public static void startAlbumActivity(Context context, String name, int id) {
        Intent intent = new Intent(context, AlbumActivity.class);
        intent.putExtra(ActivityConstant.ALBUM_OWNER_ID, id);
        intent.putExtra(ActivityConstant.ALBUM_OWNER_NAME, name);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_album;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        String name = getIntent().getStringExtra(ActivityConstant.ALBUM_OWNER_NAME);
        String title = TextUtils.isEmpty(name) ? getResources().getString(R.string.album)
                : name + "的" + getResources().getString(R.string.album);
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(title)
                .build();
    }

    @Override
    public void initComponent() {
        DaggerAlbumComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .getAlbumModule(new GetAlbumModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initRecyclerView();
        LoadingDialogManager.getInstance().showLoadingDialog(this);
        getAlbum(0);
    }

    @Override
    public void getAlbumSuccess(List<AlbumModel> list) {
        if (refreshLayout.isLoading()) {
            albumAdapter.add(list);
            refreshLayout.loadMoreFinish(list.size() > 0 ? State.LOADING_SUCCEED : State.LOAD_ALL);
        } else {
            albumAdapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
    }

    @Override
    public void getAlbumFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    private void initRecyclerView() {
        list.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
        albumAdapter = new AlbumAdapter(null);
        list.setAdapter(albumAdapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getAlbum(0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getAlbum(albumAdapter.getLastItemId());
                    }
                }).build();
    }

    /**
     * 获取相册
     *
     * @param lastId
     */
    private void getAlbum(int lastId) {
        presenter.getAlbum(getIntent().getIntExtra(ActivityConstant.ALBUM_OWNER_ID, -1), lastId);
    }
}
